﻿function PrimarryPollutantAnalyseBar(container, config) {

    this.series = [];
    this.seriesParam = config["seriesParam"] || [];
    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;
    this.stationName = config["stationName"] || "";
    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];
    this.xCate = config["xCate"] || [];


    this.container = container;
}

PrimarryPollutantAnalyseBar.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table class="table_tooltip">';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td  style="width:40px;"><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td style="width:30px;">' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;
};


PrimarryPollutantAnalyseBar.prototype.getStep = function () {
    var _ = this,
        width = $("#" + _.container).width(),
        count = _.TimePoint.length,
        num = 1,
        xWidth = _.xwidth;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

PrimarryPollutantAnalyseBar.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            type: 'column',
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        lang: {
            contextButtonTitle: '导出图片',
            loading: '加载中',
            noData: '没有数据'
        },
        title: {
            text: '首要污染物准确性比例图'
        },

        xAxis: [{
            categories: _.xCate
        }],
        yAxis: {
            min: 0,
            title: {
                text: '百分比(%)'
            }
            
        },
        plotOptions: {
            column: {
                stacking: 'normal'
               
            }
        },
        tooltip: {
            shared: true,
            //crosshairs: true,
            crosshairs: [
             {
               width: 1,
             }
            ],
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        series: _.series
    }
};

PrimarryPollutantAnalyseBar.prototype.onChartLoad = function () {
    this.removeLogo();
};

PrimarryPollutantAnalyseBar.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

PrimarryPollutantAnalyseBar.prototype.initData = function (jsonData) {
    var _ = this;

    //转换为百分比
    for (var i = 0; i < jsonData[0].length; i++) {
        var tempdata0 = jsonData[0][i];
        var tempdata1 = jsonData[1][i];

        jsonData[0][i] = Math.round((tempdata0 / (tempdata0 + tempdata1)) * 100,0);
        jsonData[1][i] = Math.round((tempdata1 / (tempdata0 + tempdata1))*100,0);
    }

    _.series.push({
        name: "√",
        color: "#52b53e",
        data: jsonData[0]
    });
    _.series.push({
        name: "×",
        color: "red",
        data: jsonData[1]
    });
    _.createChart();
};

PrimarryPollutantAnalyseBar.prototype.removeLogo = function () {
    var chartText = $("text[text-anchor='end']");
    chartText.each(function () {
        if ($(this).text() == 'Highcharts.com') {
            $(this).remove();
        }
    });
}
